package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void insert(Employee employee, Long[] roleIds) {


        employeeMapper.insert(employee);
        if (roleIds != null) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        employeeMapper.deleteRelation(id);

        employeeMapper.deleteByPrimaryKey(id);

    }

    @Override
    public Employee selectByPrimaryKey(Long id) {

        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }


    @Override
    public void updateByPrimaryKey(Employee employee, Long[] roleIds) {
        employeeMapper.deleteRelation(employee.getId());
        employeeMapper.updateByPrimaryKey(employee);
        if (roleIds != null && roleIds.length > 0) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPagSize());
        return new PageInfo<>(employeeMapper.queryForList(qo));
    }


    @Override
    public void batchDelete(Long[] ids) {
        employeeMapper.batchDelete(ids);
    }

    @Override
    public Employee queryByUsername(String username) {
        return employeeMapper.queryByUsername(username);
    }

    @Override
    public Workbook exportXls() {
        //查询所有员工
        List<Employee> employees = selectAll();
       //new一个worbook
        HSSFWorkbook workbook = new HSSFWorkbook();
      //文档名字
        HSSFSheet sheet = workbook.createSheet("员工");

        // 创建头行
        Row headRow = sheet.createRow(0);
        headRow.createCell(0).setCellValue("名称");
        headRow.createCell(1).setCellValue("年龄");
        headRow.createCell(2).setCellValue("邮箱");

        // 创建集合 行
        for (int i = 1; i <= employees.size(); i++) {
            Row dataRow = sheet.createRow(i);
            Employee employee = employees.get(i - 1);
            dataRow.createCell(0).setCellValue(employee.getName());
            dataRow.createCell(1).setCellValue(employee.getAge());
            dataRow.createCell(2).setCellValue(employee.getEmail());
        }
        return workbook;
    }

    @Override
    public void importXls(InputStream inputStream) throws IOException {
        // 上传文件是不是在这个 inputStream
        Workbook wb = WorkbookFactory.create(inputStream);
        // workbook 有是有内容
        Sheet sheet = wb.getSheetAt(0);

        // 获取这个工作簿里面有多少行
        int lastRowIndex = sheet.getLastRowNum();
        for(int i = 1; i <= lastRowIndex; i++){
            Row row = sheet.getRow(i);

            Employee e = new Employee();
            String name = row.getCell(0).getStringCellValue();
            int age = new Double(row.getCell(1).getNumericCellValue()).intValue();
            String email = row.getCell(2).getStringCellValue();

            e.setName(name);
            e.setAge(age);
            e.setEmail(email);
            e.setPassword(new SimpleHash("md5", "1", name).toString());

            employeeMapper.insert(e);
        }

    }

    @Override
    public List<Employee> queryByRoleSn(String[] roleSns) {
        return employeeMapper.queryByRoleSn(roleSns);
    }
}


